package com.menu.web.controller.typetemplate;

import com.menu.common.annotation.Log;
import com.menu.common.core.controller.BaseController;
import com.menu.common.core.domain.AjaxResult;
import com.menu.common.core.domain.entity.MenuProtype;
import com.menu.common.core.page.TableDataInfo;
import com.menu.common.enums.BusinessType;
import com.menu.common.enums.DataSourceType;
import com.menu.common.utils.poi.ExcelUtil;
import com.menu.framework.datasource.DynamicDataSourceContextHolder;
import com.menu.system.domain.MenuProductBasicattribute;
import com.menu.system.domain.MenuProductTypetemplate;
import com.menu.system.domain.vo.OrderVo;
import com.menu.system.service.IMenuProductBasicattributeService;
import com.menu.system.service.IMenuProductTypetemplateService;
import com.menu.system.service.IMenuProtypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 产品类型模板Controller
 *
 * @author ruoyi
 * @date 2024-07-08
 */
@RestController
@RequestMapping("/system/typetemplate")
public class MenuProductTypetemplateController extends BaseController {
    @Autowired
    private IMenuProductTypetemplateService menuProductTypetemplateService;
    @Autowired
    private IMenuProtypeService menuProtypeService;
    @Autowired
    private IMenuProductBasicattributeService menuProductBasicattributeService;

    /**
     * 查询产品类型模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:typetemplate:list')")
    @GetMapping("/list")
    public TableDataInfo list(MenuProductTypetemplate menuProductTypetemplate) {
        startPage();
        List<MenuProductTypetemplate> list = menuProductTypetemplateService.selectMenuProductTypetemplateList(menuProductTypetemplate);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:typetemplate:list')")
    @GetMapping("/typetemplates")
    public AjaxResult typetemplates() {


        return AjaxResult.success(menuProductTypetemplateService.selectMenuProductTypetemplateList(null));
    }

    /*查询模板配置*/
    @PreAuthorize("@ss.hasPermi('system:typetemplate:list')")
    @GetMapping("/typetemplateList")
    public AjaxResult typetemplateList() {

        AjaxResult ajaxResult = new AjaxResult();
        MenuProtype menuProtype = new MenuProtype();
        menuProtype.setParentId(0L);
        List<MenuProtype> MenuProtypeList = menuProtypeService.selectMenuProtypeList(menuProtype);
        List<MenuProductBasicattribute> MenuProductBasicattributeList = menuProductBasicattributeService.selectMenuProductBasicattributeList(null);
        ajaxResult.put("MenuProtypeList", MenuProtypeList);
        ajaxResult.put("MenuProductBasicattributeList", MenuProductBasicattributeList);

        return ajaxResult;

    }

    @PreAuthorize("@ss.hasPermi('system:typetemplate:list')")
    @GetMapping(value = "/find/{protypeId}")
    public AjaxResult typetemplateList(@PathVariable("protypeId") Long protypeId) {

        return menuProductTypetemplateService.typetemplateList(protypeId);
    }

    /**
     * 导出产品类型模板列表
     */
    @PreAuthorize("@ss.hasPermi('system:typetemplate:export')")
    @Log(title = "产品类型模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MenuProductTypetemplate menuProductTypetemplate) {
        List<MenuProductTypetemplate> list = menuProductTypetemplateService.selectMenuProductTypetemplateList(menuProductTypetemplate);
        ExcelUtil<MenuProductTypetemplate> util = new ExcelUtil<MenuProductTypetemplate>(MenuProductTypetemplate.class);
        util.exportExcel(response, list, "产品类型模板数据");
    }

    /**
     * 获取产品类型模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:typetemplate:query')")
    @GetMapping(value = "/{protypeId}")
    public AjaxResult getInfo(@PathVariable("protypeId") Long protypeId) {
        AjaxResult ajaxResult = new AjaxResult();
        List<Long> collect = menuProductTypetemplateService.selectMenuProductTypetemplateByProtypeId(protypeId).stream().map(m -> m.getBasicattributeId()).collect(Collectors.toList());
        ajaxResult.put("basicattributeIds", collect);
        ajaxResult.put("publicattributeIds", menuProductTypetemplateService.findPublicProperty(protypeId));
        ajaxResult.put("protypeId", protypeId);
        return ajaxResult;
    }

    /**
     * 新增产品类型模板
     */
    @PreAuthorize("@ss.hasPermi('system:typetemplate:add')")
    @Log(title = "产品类型模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MenuProductTypetemplate menuProductTypetemplate) {


        if (menuProductTypetemplate.getProtypeId().equals(0L)) {
            if (DynamicDataSourceContextHolder.getDataSourceType().equals(DataSourceType.MASTER.name())) {
                return error("顶级节点不可选");
            } else {
                return error("The top-level node is unavailable");
            }

        }
        if (!menuProductTypetemplateService.selectMenuProductTypetemplateByProtypeId(menuProductTypetemplate.getProtypeId()).isEmpty() && !menuProductTypetemplateService.findPublicProperty(menuProductTypetemplate.getProtypeId()).isEmpty()) {

            if (DynamicDataSourceContextHolder.getDataSourceType().equals(DataSourceType.MASTER.name())) {
                return error("模板已存在");
            } else {
                return error("Template already exists");
            }

        }


        return toAjax(menuProductTypetemplateService.insertMenuProductTypetemplate(menuProductTypetemplate));
    }

    @PreAuthorize("@ss.hasPermi('system:typetemplate:query')")
    @GetMapping(value = "/order/{protypeId}")
    public AjaxResult getInforder(@PathVariable("protypeId") Long protypeId) {
        AjaxResult ajaxResult = new AjaxResult();
        List<OrderVo> orderVos = menuProductTypetemplateService.selectMenuPublicrderVo(protypeId);
        orderVos.addAll(menuProductTypetemplateService.selectMenuProductTypetemplateOrderVo(protypeId));
        ajaxResult.put("orderVos", orderVos);

        ajaxResult.put("protypeId", protypeId);
        return ajaxResult;
    }


    @PreAuthorize("@ss.hasPermi('system:typetemplate:edit')")
    @Log(title = "排序类型模板", businessType = BusinessType.UPDATE)
    @PutMapping("/order")
    public AjaxResult order(@RequestBody List<OrderVo> orderVos) {

        return success(menuProductTypetemplateService.updateMenuProductTypetemplateOrder(orderVos));
    }

    /**
     * 修改产品类型模板
     */
    @PreAuthorize("@ss.hasPermi('system:typetemplate:edit')")
    @Log(title = "产品类型模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MenuProductTypetemplate menuProductTypetemplate) {
        return toAjax(menuProductTypetemplateService.updateMenuProductTypetemplate(menuProductTypetemplate));
    }

    /**
     * 删除产品类型模板
     */
    @PreAuthorize("@ss.hasPermi('system:typetemplate:remove')")
    @Log(title = "产品类型模板", businessType = BusinessType.DELETE)
    @DeleteMapping("/{protypeIds}")
    public AjaxResult remove(@PathVariable Long[] protypeIds) {
        return toAjax(menuProductTypetemplateService.deleteMenuProductTypetemplateByProtypeIds(protypeIds));
    }
}
